package com.captain.lc.动态规划;

import java.util.Objects;

/**
 * Des:
 *
 * @author XL
 * @Date 2021/10/22 9:01
 */
public class   MaxSubArrayOffer42 {

    public int maxSubArray(int[] nums) {
        if (Objects.isNull(nums)) return 0;
        if (nums.length == 1) return nums[0];
        //dp[i] 以nums[i] 结尾的最大连续子序列和
        int[] dp = new int[nums.length + 1];
        //初始化数据
        int max = dp[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            //dp[i] , 看dp[i - 1] 的值是否>0来进行判断
            int pre = dp[i - 1];
            if (pre <= 0) {
                dp[i] = nums[i];
            } else {
                dp[i] = pre + nums[i];
            }
            max = Math.max(max, dp[i]);
        }
        return max;
    }
}
